<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>mainwindow.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-tools-settingseditor-example.html">Settings Editor Example</a></td><td >mainwindow.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">mainwindow.cpp 示例文件</h1>
<span class="subtitle">tools/settingseditor/mainwindow.cpp</span>
<!-- $$$tools/settingseditor/mainwindow.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &lt;QtWidgets&gt;</span>

  <span class="preprocessor">#include &quot;locationdialog.h&quot;</span>
  <span class="preprocessor">#include &quot;mainwindow.h&quot;</span>
  <span class="preprocessor">#include &quot;settingstree.h&quot;</span>

  MainWindow<span class="operator">::</span>MainWindow()
      : settingsTree(<span class="keyword">new</span> SettingsTree)
      <span class="operator">,</span> locationDialog(nullptr)
  {
      setCentralWidget(settingsTree);

      createActions();

      autoRefreshAct<span class="operator">-</span><span class="operator">&gt;</span>setChecked(<span class="keyword">true</span>);
      fallbacksAct<span class="operator">-</span><span class="operator">&gt;</span>setChecked(<span class="keyword">true</span>);

      setWindowTitle(<span class="type"><a href="../qtcore/qcoreapplication.html">QCoreApplication</a></span><span class="operator">::</span>applicationName());
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qrect.html">QRect</a></span> availableGeometry <span class="operator">=</span> <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>desktop()<span class="operator">-</span><span class="operator">&gt;</span>availableGeometry(<span class="keyword">this</span>);
      adjustSize();
      move((availableGeometry<span class="operator">.</span>width() <span class="operator">-</span> width()) <span class="operator">/</span> <span class="number">2</span><span class="operator">,</span> (availableGeometry<span class="operator">.</span>height() <span class="operator">-</span> height()) <span class="operator">/</span> <span class="number">2</span>);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>openSettings()
  {
      <span class="keyword">if</span> (<span class="operator">!</span>locationDialog)
          locationDialog <span class="operator">=</span> <span class="keyword">new</span> LocationDialog(<span class="keyword">this</span>);

      <span class="keyword">if</span> (locationDialog<span class="operator">-</span><span class="operator">&gt;</span>exec() <span class="operator">!</span><span class="operator">=</span> <span class="type"><a href="qdialog.html">QDialog</a></span><span class="operator">::</span>Accepted)
          <span class="keyword">return</span>;

      SettingsPtr settings(<span class="keyword">new</span> <span class="type"><a href="../qtcore/qsettings.html">QSettings</a></span>(locationDialog<span class="operator">-</span><span class="operator">&gt;</span>format()<span class="operator">,</span>
                                         locationDialog<span class="operator">-</span><span class="operator">&gt;</span>scope()<span class="operator">,</span>
                                         locationDialog<span class="operator">-</span><span class="operator">&gt;</span>organization()<span class="operator">,</span>
                                         locationDialog<span class="operator">-</span><span class="operator">&gt;</span>application()));

      setSettingsObject(settings);
      fallbacksAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>openIniFile()
  {
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> directory <span class="operator">=</span> <span class="type"><a href="../qtcore/qstandardpaths.html">QStandardPaths</a></span><span class="operator">::</span>writableLocation(<span class="type"><a href="../qtcore/qstandardpaths.html">QStandardPaths</a></span><span class="operator">::</span>ConfigLocation);
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> fileName <span class="operator">=</span>
          <span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>getOpenFileName(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Open INI File&quot;</span>)<span class="operator">,</span>
                                       directory<span class="operator">,</span> tr(<span class="string">&quot;INI Files (*.ini *.conf)&quot;</span>));
      <span class="keyword">if</span> (fileName<span class="operator">.</span>isEmpty())
          <span class="keyword">return</span>;

      SettingsPtr settings(<span class="keyword">new</span> <span class="type"><a href="../qtcore/qsettings.html">QSettings</a></span>(fileName<span class="operator">,</span> <span class="type"><a href="../qtcore/qsettings.html">QSettings</a></span><span class="operator">::</span>IniFormat));

      setSettingsObject(settings);
      fallbacksAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>openPropertyList()
  {
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> directory <span class="operator">=</span> <span class="type"><a href="../qtcore/qstandardpaths.html">QStandardPaths</a></span><span class="operator">::</span>writableLocation(<span class="type"><a href="../qtcore/qstandardpaths.html">QStandardPaths</a></span><span class="operator">::</span>ConfigLocation);
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> fileName <span class="operator">=</span>
          <span class="type"><a href="qfiledialog.html">QFileDialog</a></span><span class="operator">::</span>getOpenFileName(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Open Property List&quot;</span>)<span class="operator">,</span>
                                       directory<span class="operator">,</span> tr(<span class="string">&quot;Property List Files (*.plist)&quot;</span>));
      <span class="keyword">if</span> (fileName<span class="operator">.</span>isEmpty())
          <span class="keyword">return</span>;

      SettingsPtr settings(<span class="keyword">new</span> <span class="type"><a href="../qtcore/qsettings.html">QSettings</a></span>(fileName<span class="operator">,</span> <span class="type"><a href="../qtcore/qsettings.html">QSettings</a></span><span class="operator">::</span>NativeFormat));
      setSettingsObject(settings);
      fallbacksAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>openRegistryPath()
  {
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> path <span class="operator">=</span>
          <span class="type"><a href="qinputdialog.html">QInputDialog</a></span><span class="operator">::</span>getText(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Open Registry Path&quot;</span>)<span class="operator">,</span>
                                tr(<span class="string">&quot;Enter the path in the Windows registry:&quot;</span>)<span class="operator">,</span>
                                <span class="type"><a href="qlineedit.html">QLineEdit</a></span><span class="operator">::</span>Normal<span class="operator">,</span> <span class="string">&quot;HKEY_CURRENT_USER\\&quot;</span>);
      <span class="keyword">if</span> (path<span class="operator">.</span>isEmpty())
          <span class="keyword">return</span>;

      SettingsPtr settings(<span class="keyword">new</span> <span class="type"><a href="../qtcore/qsettings.html">QSettings</a></span>(path<span class="operator">,</span> <span class="type"><a href="../qtcore/qsettings.html">QSettings</a></span><span class="operator">::</span>NativeFormat));

      setSettingsObject(settings);
      fallbacksAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>about()
  {
      <span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>about(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;About Settings Editor&quot;</span>)<span class="operator">,</span>
              tr(<span class="string">&quot;The &lt;b&gt;Settings Editor&lt;/b&gt; example shows how to access &quot;</span>
                 <span class="string">&quot;application settings using Qt.&quot;</span>));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>createActions()
  {
      <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>fileMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;File&quot;</span>));

      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>openSettingsAct <span class="operator">=</span> fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;&amp;Open Application Settings...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>openSettings);
      openSettingsAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Open);

      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>openIniFileAct <span class="operator">=</span> fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Open I&amp;NI File...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>openIniFile);
      openIniFileAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(tr(<span class="string">&quot;Ctrl+N&quot;</span>));

  <span class="preprocessor">#ifdef Q_OS_OSX</span>
      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>openPropertyListAct <span class="operator">=</span> fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Open Apple &amp;Property List...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>openPropertyList);
      openPropertyListAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(tr(<span class="string">&quot;Ctrl+P&quot;</span>));
  <span class="preprocessor">#endif // Q_OS_OSX</span>

  <span class="preprocessor">#ifdef Q_OS_WIN</span>
      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>openRegistryPathAct <span class="operator">=</span> fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;Open Windows &amp;Registry Path...&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>openRegistryPath);
      openRegistryPathAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(tr(<span class="string">&quot;Ctrl+G&quot;</span>));
  <span class="preprocessor">#endif // Q_OS_WIN</span>

      fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();

      refreshAct <span class="operator">=</span> fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;&amp;Refresh&quot;</span>)<span class="operator">,</span> settingsTree<span class="operator">,</span> <span class="operator">&amp;</span>SettingsTree<span class="operator">::</span>refresh);
      refreshAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(tr(<span class="string">&quot;Ctrl+R&quot;</span>));
      refreshAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);

      fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addSeparator();

      <span class="type"><a href="qaction.html">QAction</a></span> <span class="operator">*</span>exitAct <span class="operator">=</span> fileMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;E&amp;xit&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qwidget.html">QWidget</a></span><span class="operator">::</span>close);
      exitAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcuts(<span class="type"><a href="../qtgui/qkeysequence.html">QKeySequence</a></span><span class="operator">::</span>Quit);

      <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>optionsMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;Options&quot;</span>));

      autoRefreshAct <span class="operator">=</span> optionsMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;&amp;Auto-Refresh&quot;</span>));
      autoRefreshAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(tr(<span class="string">&quot;Ctrl+A&quot;</span>));
      autoRefreshAct<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      autoRefreshAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
      connect(autoRefreshAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span>
              settingsTree<span class="operator">,</span> <span class="operator">&amp;</span>SettingsTree<span class="operator">::</span>setAutoRefresh);
      connect(autoRefreshAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span>
              refreshAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>setDisabled);

      fallbacksAct <span class="operator">=</span> optionsMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;&amp;Fallbacks&quot;</span>));
      fallbacksAct<span class="operator">-</span><span class="operator">&gt;</span>setShortcut(tr(<span class="string">&quot;Ctrl+F&quot;</span>));
      fallbacksAct<span class="operator">-</span><span class="operator">&gt;</span>setCheckable(<span class="keyword">true</span>);
      fallbacksAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">false</span>);
      connect(fallbacksAct<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qaction.html">QAction</a></span><span class="operator">::</span>triggered<span class="operator">,</span>
              settingsTree<span class="operator">,</span> <span class="operator">&amp;</span>SettingsTree<span class="operator">::</span>setFallbacksEnabled);

      <span class="type"><a href="qmenu.html">QMenu</a></span> <span class="operator">*</span>helpMenu <span class="operator">=</span> menuBar()<span class="operator">-</span><span class="operator">&gt;</span>addMenu(tr(<span class="string">&quot;&amp;Help&quot;</span>));
      helpMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;&amp;About&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>MainWindow<span class="operator">::</span>about);
      helpMenu<span class="operator">-</span><span class="operator">&gt;</span>addAction(tr(<span class="string">&quot;About &amp;Qt&quot;</span>)<span class="operator">,</span> <a href="qapplication.html#qApp">qApp</a><span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="../qtcore/qcoreapplication.html">QCoreApplication</a></span><span class="operator">::</span>quit);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>setSettingsObject(<span class="keyword">const</span> SettingsPtr <span class="operator">&amp;</span>settings)
  {
      settings<span class="operator">-</span><span class="operator">&gt;</span>setFallbacksEnabled(fallbacksAct<span class="operator">-</span><span class="operator">&gt;</span>isChecked());
      settingsTree<span class="operator">-</span><span class="operator">&gt;</span>setSettingsObject(settings);

      refreshAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);
      autoRefreshAct<span class="operator">-</span><span class="operator">&gt;</span>setEnabled(<span class="keyword">true</span>);

      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> niceName <span class="operator">=</span> <span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>cleanPath(settings<span class="operator">-</span><span class="operator">&gt;</span>fileName());
      <span class="type">int</span> pos <span class="operator">=</span> niceName<span class="operator">.</span>lastIndexOf(QLatin1Char(<span class="char">'/'</span>));
      <span class="keyword">if</span> (pos <span class="operator">!</span><span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>)
          niceName<span class="operator">.</span>remove(<span class="number">0</span><span class="operator">,</span> pos <span class="operator">+</span> <span class="number">1</span>);

      <span class="keyword">if</span> (<span class="operator">!</span>settings<span class="operator">-</span><span class="operator">&gt;</span>isWritable())
          niceName <span class="operator">=</span> tr(<span class="string">&quot;%1 (read only)&quot;</span>)<span class="operator">.</span>arg(niceName);

      setWindowTitle(tr(<span class="string">&quot;%1 - %2&quot;</span>)<span class="operator">.</span>arg(niceName<span class="operator">,</span> <span class="type"><a href="../qtcore/qcoreapplication.html">QCoreApplication</a></span><span class="operator">::</span>applicationName()));
      statusBar()<span class="operator">-</span><span class="operator">&gt;</span>showMessage(tr(<span class="string">&quot;Opened \&quot;%1\&quot;&quot;</span>)<span class="operator">.</span>arg(<span class="type"><a href="../qtcore/qdir.html">QDir</a></span><span class="operator">::</span>toNativeSeparators(settings<span class="operator">-</span><span class="operator">&gt;</span>fileName())));
  }

</pre>
</div>
<!-- @@@tools/settingseditor/mainwindow.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
